package com.mr.security.web.commons.config;

import com.mr.security.web.commons.validate.IValidateCodeGenerator;
import com.mr.security.web.commons.validate.imagecode.ImageValidateCodeGeneratorImpl;
import com.mr.security.web.commons.validate.smscode.DefaultSmsCodeSenderImpl;
import com.mr.security.web.commons.validate.smscode.SmsCodeSender;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Class: ValidateCodeBeanConfig
 * User (作者):MRui
 * TODO (描述)：验证码配置类（相当于在接口类加@Component之所以不用原因详见注释）
 * Date 2019-01-22 23:37
 */
@Configuration
public class ValidateCodeBeanConfig {
    //属性配置类
    @Autowired
    private SecurityPropertiesConfig securityProperties;

    /**
    * User (作者): MRui
    * TODO (描述)：因为ConditionalOnMissingBean注解作用为当系统中找不到imageCodeGenerator的Bean时
     * 调用此方法（假如有人实现了验证码实现类注入了imageCodeGenerator时，就会使用新的实现方法
     * 注入方式为@Component（“imageCodeGenerator”））
    * Data：2019-01-22 23:42
    */
    @Bean
    @ConditionalOnMissingBean(name = "imageIValidateCodeGenerator")//由于图片验证码有多种实现方式故不将实现类注入（便于修改）(方法名要与bean名字一致)
    //@ConditionalOnMissingBean(ValidateCodeGenerator.class)//与上同理，系统找到此接口的实现就会调用实现的方法
    public IValidateCodeGenerator imageIValidateCodeGenerator(){
        ImageValidateCodeGeneratorImpl codeGenerator = new ImageValidateCodeGeneratorImpl();
        codeGenerator.setSecurityProperties(securityProperties);
        return codeGenerator;
    }

    /**
    * User (作者): MRui
    * TODO (描述)：短信验证码的配置同图形验证码作用
    * Data：2019-01-24 23:59
    */
    @Bean
    //@ConditionalOnMissingBean(name = "smsCodeSend")
    @ConditionalOnMissingBean(SmsCodeSender.class)//与上同理，系统找到此接口的实现就会调用实现的方法
    public SmsCodeSender smsCodeSender(){
        return new DefaultSmsCodeSenderImpl();
    }
}
